<?php
namespace app\Admin\controller;
use app\Admin\model\Advert as AdvertModel;
use app\admin\controller\Base;

class Advert extends Base
{
    public function index()
    {
        $advert=new AdvertModel();
        if(request()->isPost()){
            $sorts=input('post.');

            foreach ($sorts as $k => $v) {
                $advert->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('index'));
            return;
        }
        $advertres=$advert->order('sort desc')->paginate(3);
        $this->assign('list',$advertres);
        return view();
    }

    public function add()
    {   
        if(request()->isPost()){
            $data=input('post.');
            $data['addtime']=time();
            $validate = \think\Loader::validate('advert');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $advert=new AdvertModel;
            if($advert->save($data)){
                $this->success('添加广告成功',url('index'));
            }else{
                $this->error('添加广告失败！');
            }
            return;
        }
        return view();
    }

    public function edit(){
         if(request()->isPost()){
            $data=input('post.');
            $validate = \think\Loader::validate('advert');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $advert = new AdvertModel;
            if(!in_array('status', $data)){
                $data['status']=0;
            }
            $save=$advert->update($data);
            if($save){
                $this->success('修改广告成功！',url('index'));
            }else{
                $this->error('修改广告失败！');
            }
            return;
        }
        $list=db('advert')->where(array('id'=>input('id')))->find();
        $this->assign('list',$list);
        return view();
    }

    public function onstatus()
    {
        $id = input('id');
        $advert = db('advert')->find($id);
        if ($advert['status'] == 1) {
            $data = [
                'id' => $id,
                'status' => 0,
            ];
            db('advert')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'status' => 1,
            ];
            db('advert')->update($data);
            $this->redirect('index');
        }
    }

    public function ontype()
    {
        $id = input('id');
        $advert = db('advert')->find($id);
        if ($advert['type'] == 1) {
            $data = [
                'id' => $id,
                'type' => 0,
            ];
            db('advert')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'type' => 1,
            ];
            db('advert')->update($data);
            $this->redirect('index');
        }
    }

    public function del(){
        $id=input('id');
        if(db('advert')->delete(input('id'))){
            $this->success('删除广告成功！','index');
        }else{
            $this->error('删除广告失败！');
        }
        
    }



}
